﻿Public Class frmEleves
    Private szSQL As String
    Dim szCritere As String
    Private rs As ADODB.Recordset


    Private Sub frmEleves_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        TE_NOM.Clear()
        ChkCLOTURE.Checked = False
        InitListeEleves(0)

    End Sub

    Private Sub InitListeEleves(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_ELEVES.Items.Clear()

        If TE_NOM.Text = "" Then
            ' afiche les 50 derniers ELEVES crées par l'agence
            szSQL = "CALL spGet_ELEVES_byCRITERES('" & gUserAgence & "'," & CInt(ChkCLOTURE.Checked) & ")"
        Else
            ' affiche les 50 ELEVES à partir de l'élève courant
            szSQL = "CALL spGet_ELEVES_byCRITERES('" & TE_NOM.Text & "'," & CInt(ChkCLOTURE.Checked) & ")"
        End If
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            ' ignorer les activités
            If nv(rs.Fields("permis_demande").Value) <> "ACTIV" Then
                LV_ELEVES.Items.Add(rs.Fields("cd_cli").Value)
                LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("civilite").Value)
                LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("nompren").Value)
                LV_ELEVES.Items(nb).SubItems.Add(nv(rs.Fields("permis_demande").Value))
                LV_ELEVES.Items(nb).SubItems.Add(nv(rs.Fields("formule").Value))
                LV_ELEVES.Items(nb).SubItems.Add(Zero2Space(rs.Fields("solde").Value))
                LV_ELEVES.Items(nb).SubItems.Add(Format(rs.Fields("statut").Value, "00"))
                If rs.Fields("solde").Value < 0 Then
                    LV_ELEVES.Items(nb).ForeColor = Color.Red
                End If
                If nv(rs.Fields("statut").Value) > 0 Then
                    LV_ELEVES.Items(nb).ForeColor = Color.Gray
                End If
                nb = nb + 1
            End If
            ' Get the next record
            rs.MoveNext()
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        If nb > 1 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
        Else
            LB_NbLignes.Text = CStr(nb) & " ligne"
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_ELEVES)

    End Sub

    Private Sub LV_ELEVES_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_ELEVES.DoubleClick
        If LV_ELEVES.SelectedItems.Count > 0 Then
            gCD_CLI = LV_ELEVES.SelectedItems(0).Text
            ' afficher les détails 
            frmElevesMAJ.ShowDialog()
            ' réaffiche la viewlist
            InitListeEleves(LV_ELEVES.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_ELEVES_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_ELEVES.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_ELEVES_DoubleClick(sender, e)

            Case Keys.Insert        ' Insert = ' Ajouter un élève
                BO_ADD_LIGNE_Click(sender, e)
        End Select

    End Sub

    Private Sub TE_NOM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TE_NOM.KeyDown
        If e.KeyCode = Keys.Enter Then
            ' affiche la listview
            InitListeEleves(0)
        End If
    End Sub


    Private Sub ChkCLOTURE_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChkCLOTURE.CheckedChanged
        ' affiche la listview
        InitListeEleves(0)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' utilisateur autorisé à créer un élève ?
        If gUserAcces < 9 Then
            MsgBox("Pour créer une fiche élève, veuillez passer par le module PROSPECTS !", MsgBoxStyle.Exclamation, "PROSPECTS")
            Exit Sub
        End If
        gCD_CLI = 0
        frmElevesMAJ.ShowDialog()
        ' afiche les 50 derniers ELEVES crées par l'agence
        szCritere = "where agence = '" & gUserAgence & "' ORDER BY modif_le DESC limit 50"
        InitListeEleves(0)
    End Sub
End Class